﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Dao.Bd;
using FreedomERP.Dao.Bd.Impl;
using FreedomERP.Models;
using System.Collections;
using FreedomERP.Comm;
using FreedomERP.Comm.Utils;

namespace FreedomERP.Services.Bd.Impl
{
    public class BdAccountServiceImpl:IBdAccountService
    {
        IBdAccountDao accountDao = new BdAccountDaoImpl();

        /*
         * 根据条件获取帐号列表
         */
        public List<BdAccount> QueryAccountPaggingByParam(PagingParams paggingParams, out int totalCount)
        {
            return accountDao.QueryAccountPaggingByParam(paggingParams,out totalCount);
        }

        /*
         * 根据帐号获取信息
         */
        public BdAccount GetAccountByFAccount(String account) 
        {
            BdAccount bdAccount = null;
            bdAccount = accountDao.GetAccountByFAccount(account);
            return bdAccount;
        }

        /*
        * 根据FId获取信息
        */
        public BdAccount GetAccountByFId(String FId)
        {
            BdAccount bdAccount = null;
            bdAccount = accountDao.GetAccountByFId(FId);
            return bdAccount;
        }

        /*
        * 添加帐号
        */
        public void AddAccount(BdAccount account)
        {
            accountDao.AddAccount(account);
            accountDao.Save();
        }

        /*
        * 修改帐号
        */
        public void ModifyAccount(BdAccount account)
        {
            BdAccount accountTemp = accountDao.GetAccountByFId(account.FId);
            if (accountTemp != null)
            {
                accountTemp.FAccount = account.FAccount;
                if (!accountTemp.FPassword.Equals(account.FPassword))
                {
                    accountTemp.FPassword = MD5Utils.MD5Encrypt(account.FPassword);
                }
                accountTemp.FRoleId = account.FRoleId;
                accountTemp.FStatusId = account.FStatusId;

                accountDao.Save();
            }
        }

        /*
        * 删除帐号
        */
        public void DeleteAccount(String FId)
        {
            BdAccount accountTemp = accountDao.GetAccountByFId(FId);
            if (accountTemp != null)
            {
                accountDao.DeleteAccount(accountTemp);
                accountDao.Save();
            }
        }

        /*
        * 修改密码
        */
        public void ChangePassword(String FId, String FPassword)
        {
            BdAccount accountTemp = accountDao.GetAccountByFId(FId);
            if (accountTemp != null)
            {
                accountTemp.FPassword = FPassword;
                accountDao.Save();
            }
        }
    }
}